package lib

import (
	"strconv"
	"strings"
)

func init() {
	Probs = append(Probs, Problem{
		Num:         38,
		Discription: "第n个外观数列",
		Level:       2,
		Labels: map[string]int{
			"递归":  1,
			"字符串": 1,
		},
	})
}

func CountAndSay(n int) string {
	str := "1"
	for i := 1; i < n; i++ {
		var builder strings.Builder
		for j := 0; j < len(str); j++ {
			count := 1
			for j+1 < len(str) && str[j] == str[j+1] {
				j++
				count++
			}

			builder.WriteString(strconv.Itoa(count))
			builder.WriteByte(str[j])
		}
		str = builder.String()
	}

	return str
}
